package org.smartercontext.local.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.contextsphere.ContextSphereController;


/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        	String name=request.getParameter("name");
        	String email=request.getParameter("email");
        	String username=request.getParameter("username");
        	String password=request.getParameter("password");
        	String passwordR=request.getParameter("passwordR");
        	
        	if(name!=null&&email!=null&&username!=null&&password!=null&&passwordR!=null){
        		
        		if(password.equals(passwordR)){
        			
        			ContextSphereController csC=new ContextSphereController();
        			boolean result=csC.searchUsername(username);
        			
        			if(result){
        				
        				if(username.length()<16&&password.length()<16){
        					
        					ContextSphereController csc = new ContextSphereController();
        					
        					try{
        					csc.registerNewUser(username, password, name, email);
        					
        					HttpSession session = request.getSession(true);
        					session.setAttribute("username", username);
        					response.sendRedirect("index.jsp");
        					
        					}catch (Exception e) {
        						response.sendRedirect("register.jsp?Error=5");
							}
        					
        				}else{
        					response.sendRedirect("register.jsp?Error=4");
        				}
        				
        			}else{
        				response.sendRedirect("register.jsp?Error=3");
        			}
        			
        		}else{
        			response.sendRedirect("register.jsp?Error=2");
        		}
        		
        	}else{
        		response.sendRedirect("register.jsp?Error=1");
        	}
        
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
